package com.woniu.scss.service.impl;

import com.google.common.collect.Lists;

import java.math.BigDecimal;
import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.common.dto.XGFLDto;
import com.woniu.common.utils.*;
import com.woniu.scss.dto.ProductAddDTO;
import com.woniu.scss.dto.ProductDTO;
import com.woniu.scss.feign.ZyProductFeign;
import com.woniu.scss.mapper.ImgListMapper;
import com.woniu.scss.pojo.ImgList;
import com.woniu.scss.pojo.ProductType;
import com.woniu.scss.service.ImgListService;
import com.woniu.scss.service.ProductTypeService;
import com.woniu.scss.vo.PorductUpdateVO;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.scss.mapper.ProductMapper;
import com.woniu.scss.pojo.Product;
import com.woniu.scss.service.ProductService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductTypeService productTypeService;


    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ImgListService imgListService;

    @Autowired
    private ImgListMapper imgListMapper;


    @Override
    public ResponseData<?> selProductByCondition(PageParam<ProductDTO> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<Product> list = productMapper.selProductByCondition(pageParam.getParam());
        PageInfo<Product> productPageInfo = new PageInfo<Product>(list);
        return new ResponseData<>().success(productPageInfo);
    }

    @Override
    public ResponseData<?> putaway(String id) {
        Integer count = lambdaQuery()
                .eq(Product::getStatus, 2)
                .eq(Product::getId, id)
                .count();
        if (count > 0) {
            return new ResponseData<>().fail(ResponseEnum.REPEAT_PUTAWAY); //608 商品重复上架
        }

        lambdaUpdate()
                .eq(Product::getId, id)
                .set(Product::getStatus, 2)
                .update();
        return new ResponseData<>().success();
    }

    @Override
    public ResponseData<?> soldOut(String id) {
        Integer count = lambdaQuery()
                .eq(Product::getStatus, 1)
                .eq(Product::getId, id)
                .count();
        if (count > 0) {
            return new ResponseData<>().fail(ResponseEnum.REPEAT_SOLDOUT); //609 商品重复上架
        }

        lambdaUpdate()
                .eq(Product::getId, id)
                .set(Product::getStatus, 1)
                .update();
        return new ResponseData<>().success();
    }

    @Override
    public ResponseData<?> deleteProduct(List<Product> param) {
        ArrayList<String> list = new ArrayList<>();
        for (Product product : param) {
            list.add(product.getId());
        }
        removeByIds(list);
        return new ResponseData<>().success();
    }

    @Override
    public ResponseData<?> selProductSort() {
        List<ProductType> list = productTypeService.lambdaQuery()
                .eq(ProductType::getStatus, 1)
                .list();
        return new ResponseData<>().success(list);
    }

    @Transactional
    @Override
    public ResponseData<?> addProduct(ProductAddDTO productAddDTO) throws Exception {
        Product product = new Product();
        long snowId = new SnowflakeIdGenerator().nextId();
        product.setId(String.valueOf(snowId));
        product.setName(productAddDTO.getProductName());
        product.setPrice(productAddDTO.getPrice());
        product.setProductType(productAddDTO.getProductTypeId());
        product.setProductImg(productAddDTO.getProductImg());
        product.setStatus(2);
        product.setStock(productAddDTO.getStock());
        product.setEcss(0);
        product.setBusinessId(null);
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());
        String userName = GetManageUserNickName.getUserName();
        product.setUpdateBy(userName);
        product.setCumulativeSales(0);
        save(product);

        ArrayList<ImgList> addList = new ArrayList<>();

        for (String s : productAddDTO.getImgList()) {
            ImgList imgList = new ImgList();
            UUID uuid = UUID.randomUUID();
            imgList.setId(String.valueOf(uuid));
            imgList.setProductId(String.valueOf(snowId));
            imgList.setImgSrc(s);
            addList.add(imgList);
        }
        imgListService.saveBatch(addList);

        return new ResponseData<>().success();
    }

    @Override
    public ResponseData<?> selProductById(String productId) {
        PorductUpdateVO porductUpdateVO = productMapper.selProductById(productId);
        List<ImgList> list = imgListService.lambdaQuery().eq(ImgList::getProductId, productId).list();
        porductUpdateVO.setImgLists(list);
        return new ResponseData<>().success(porductUpdateVO);
    }

    @SneakyThrows
    @Override
    @Transactional
    public ResponseData<?> updateProduct(ProductAddDTO productAddDTO) {
        // 修改详情图
        LambdaQueryWrapper<ImgList> imgListQueryWrapper = new LambdaQueryWrapper<>();
        imgListQueryWrapper.eq(ImgList::getProductId, productAddDTO.getProductId());
//        imgListMapper.delete(imgListQueryWrapper);
        imgListService.remove(imgListQueryWrapper);

        ArrayList<ImgList> imgLists = new ArrayList<>();
        for (String s : productAddDTO.getImgList()) {
            ImgList imgList = new ImgList();
            imgList.setId(String.valueOf(UUID.randomUUID()));
            imgList.setProductId(productAddDTO.getProductId());
            imgList.setImgSrc(s);
            imgLists.add(imgList);
        }
        imgListService.saveBatch(imgLists);

        // 修改商品
        Product product = new Product();
        product.setId(productAddDTO.getProductId());
        product.setName(productAddDTO.getProductName());
        product.setPrice(productAddDTO.getPrice());
        product.setProductType(productAddDTO.getProductTypeId());
        product.setProductImg(productAddDTO.getProductImg());
        product.setStatus(2);
        product.setStock(productAddDTO.getStock());
        product.setEcss(0);
        product.setBusinessId(null);
        product.setUpdateTime(new Date());
        product.setUpdateBy(GetManageUserNickName.getUserName());

        updateById(product);
        return new ResponseData<>().success();
    }

    @Autowired
    private ZyProductFeign productFeign;

    @Override
    public ResponseData<?> productPush() {
        List<Product> productList = lambdaQuery().eq(Product::getStatus, 2)
                .list();
        List<ProductAddDTO> productAddDTOList = new ArrayList<>();
        for (Product product : productList) {
            ProductAddDTO productAddDTO = new ProductAddDTO();
            productAddDTO.setProductTypeId(product.getProductType());
            productAddDTO.setProductId(product.getId());
            productAddDTO.setProductName(product.getName());
            productAddDTO.setPrice(product.getPrice());
            productAddDTO.setStock(product.getStock());
            productAddDTO.setProductImg(product.getProductImg());
            productAddDTO.setStatus(product.getStatus());
            List<ImgList> imgListList = imgListService.lambdaQuery()
                    .eq(ImgList::getProductId, product.getId())
                    .list();
            ArrayList<String> imgSrcList = new ArrayList<>();
            for (ImgList imgList : imgListList) {
                imgSrcList.add(imgList.getImgSrc());
            }
            productAddDTO.setImgList(imgSrcList);
            productAddDTOList.add(productAddDTO);
        }
        Boolean b = productFeign.productPush(productAddDTOList);
        if (b) {
            return new ResponseData<>().success();
        }
        return new ResponseData<>().fail();
    }

    @Override
    public ResponseData<?> xiaJia(List<String> productId) {
        boolean update = lambdaUpdate().in(Product::getId, productId).set(Product::getStatus, 1).update();
        return update ? new ResponseData<>().success() : new ResponseData<>().fail();
    }

    @Override
    public ResponseData<?> shangJia(List<String> productId) {
        boolean update = lambdaUpdate().in(Product::getId, productId).set(Product::getStatus, 2).update();
        return update ? new ResponseData<>().success() : new ResponseData<>().fail();
    }

    @Override
    public ResponseData<?> updateProductType(XGFLDto xgfl) {
        boolean update = lambdaUpdate().in(Product::getId, xgfl.getProductId())
                .set(Product::getProductType, xgfl.getTypeId())
                .update();
        return update? new ResponseData<>().success() : new ResponseData<>().fail();
    }

    @Override
    public ResponseData<?> TBDeletePro(List<String> productId) {
        boolean b = removeByIds(productId);
        return b ? new ResponseData<>().success() : new ResponseData<>().fail();
    }
}
